﻿namespace Security_Project
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using SecutityLib;

    /// <summary>
    /// Main Form
    /// </summary>
    public partial class Form1 : Form
    {
        /// <summary>
        /// Encoding tech instance 
        /// </summary>
        private EncodingDecodingTech encodingDecodingTechnique;

        /// <summary>
        /// Initializes a new instance of the <see cref="Form1"/> class.
        /// </summary>
        public Form1()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the btnEncrypt control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnEncrypt_Click(object sender, EventArgs e)
        {
            if (this.encodingDecodingTechnique is PolyAlphabetic)
            {
                PolyAlphabetic temp = (PolyAlphabetic)this.encodingDecodingTechnique;
                if (this.checkBoxAutoKey.Checked)
                {
                    temp.RepeatKey = false;
                }
                else
                {
                    temp.RepeatKey = true;
                }
            }

            this.Encode();
        }

        /// <summary>
        /// Encodes this instance.
        /// </summary>
        private void Encode()
        {
            string plain = this.txtInput.Text;
            plain = plain.Replace(" ", string.Empty);
            plain = plain.ToLower();

            string key = this.txtKey.Text;
            key = key.Replace(" ", string.Empty);
            key = key.ToLower();

            if (plain == string.Empty)
            {
                MessageBox.Show("Enter Your plain Text");
                return;
            }
            else if (key == string.Empty && !(this.encodingDecodingTechnique is MD5))
            {
                MessageBox.Show("Enter Your Key");
                return;
            }

            try
            {
                string result = string.Empty;

                if (this.encodingDecodingTechnique is DES && this.checkBoxAutoKey.Checked)
                {
                    string[] keys = key.Split(',');
                    DES temp = (DES)this.encodingDecodingTechnique;
                    result = this.encodingDecodingTechnique.Encode(plain, keys[0].ToLower());
                    result = this.encodingDecodingTechnique.Decode(plain, keys[1].ToLower());
                    result = this.encodingDecodingTechnique.Encode(plain, keys[0].ToLower());
                }
                else
                {
                    result = this.encodingDecodingTechnique.Encode(plain, key);
                }

                this.txtOutput.Text = result;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex.Message.ToString());
            }
        }

        /// <summary>
        /// Decodes this instance.
        /// </summary>
        private void Decode()
        {
            string plain = this.txtInput.Text;
            plain = plain.Replace(" ", string.Empty);
            if (!(this.encodingDecodingTechnique is DES || this.encodingDecodingTechnique is AES))
            {
                plain = plain.ToLower();
            }

            string key = this.txtKey.Text;
            key = key.Replace(" ", string.Empty);
            key = key.ToLower();

            if (this.encodingDecodingTechnique is MD5)
            {
                MessageBox.Show("MD5 is a hashing algorithm so it's one directional, cannot apply decoding");
                return;
            }

            if (plain == string.Empty)
            {
                MessageBox.Show("Enter Your Cipher Text");
                return;
            }
            else if (key == string.Empty)
            {
                MessageBox.Show("Enter Your Key");
                return;
            }

            try
            {
                string result = string.Empty;

                if (this.encodingDecodingTechnique is DES && this.checkBoxAutoKey.Checked)
                {
                    string[] keys = key.Split(',');
                    if (key.Length != 2)
                    {
                        throw new Exception("You must enter two keys seperated by a comma (,)");
                    }

                    keys[0] = keys[0].Replace(" ", string.Empty);
                    keys[1] = keys[1].Replace(" ", string.Empty);
                    DES temp = (DES)this.encodingDecodingTechnique;
                    result = this.encodingDecodingTechnique.Decode(plain, keys[0].ToLower());
                    result = this.encodingDecodingTechnique.Encode(plain, keys[1].ToLower());
                    result = this.encodingDecodingTechnique.Decode(plain, keys[0].ToLower());
                }
                else
                {
                    result = this.encodingDecodingTechnique.Decode(plain, key);
                }

                this.txtOutput.Text = result;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error " + ex.Message.ToString());
            }
        }

        /// <summary>
        /// Handles the Click event of the btnDecrypt control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnDecrypt_Click(object sender, EventArgs e)
        {
            if (this.encodingDecodingTechnique is PolyAlphabetic)
            {
                PolyAlphabetic temp = (PolyAlphabetic)this.encodingDecodingTechnique;
                if (this.checkBoxAutoKey.Checked)
                {
                    temp.RepeatKey = false;
                }
                else
                {
                    temp.RepeatKey = true;
                }
            }

            this.Decode();
        }

        /// <summary>
        /// Handles the Click event of the btnCaesar control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnCaesar_Click(object sender, EventArgs e)
        {
            this.grbData.Visible = true;
            this.encodingDecodingTechnique = new CeaserCipher();
            this.grbData.Text = "Ceaser Cipher Encoding";
            this.checkBoxAutoKey.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the btnMono control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnMono_Click(object sender, EventArgs e)
        {
            this.grbData.Visible = true;
            this.encodingDecodingTechnique = new MonoAlphabetic();
            this.grbData.Text = "Mono Alphabetic Technique";
            this.checkBoxAutoKey.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the btnPlay control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnPlay_Click(object sender, EventArgs e)
        {
            this.grbData.Visible = true;
            this.encodingDecodingTechnique = new PlayFairCipher();
            this.grbData.Text = "Play Fair Technique";
            this.checkBoxAutoKey.Visible = false;
        }

        /// <summary>
        /// Handles the Click event of the BtnPoly control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnPoly_Click(object sender, EventArgs e)
        {
            this.grbData.Visible = true;
            this.checkBoxAutoKey.Visible = true;
            this.encodingDecodingTechnique = new PolyAlphabetic();
            this.grbData.Text = "Polyalphabetic Technique";
        }

        /// <summary>
        /// Handles the Click event of the BtnHill control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnHill_Click(object sender, EventArgs e)
        {
            this.grbData.Visible = true;
            this.checkBoxAutoKey.Visible = false;
            this.encodingDecodingTechnique = new HillCipher();
            this.grbData.Text = "Hill Cipher Technique";
        }

        /// <summary>
        /// Handles the Click event of the BtnRail control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnRail_Click(object sender, EventArgs e)
        {
            this.grbData.Visible = true;
            this.checkBoxAutoKey.Visible = false;
            this.encodingDecodingTechnique = new RailFence();
            this.grbData.Text = "Rail Fence Technique";
        }

        /// <summary>
        /// Handles the Click event of the btnColumnar control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnColumnar_Click(object sender, EventArgs e)
        {
            this.grbData.Visible = true;
            this.checkBoxAutoKey.Visible = false;
            this.encodingDecodingTechnique = new Columnar();
            this.grbData.Text = "Columnar Technique";
        }

        /// <summary>
        /// Handles the Click event of the btnDES control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnDES_Click(object sender, EventArgs e)
        {
            this.grbData.Visible = true;
            this.checkBoxAutoKey.Visible = true;
            this.checkBoxAutoKey.Text = "triple Des";
            this.encodingDecodingTechnique = new DES();
            this.grbData.Text = "DES Technique";
        }

        /// <summary>
        /// Handles the Click event of the btnAES control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnAES_Click(object sender, EventArgs e)
        {
            this.grbData.Visible = true;
            this.checkBoxAutoKey.Visible = false;
            this.encodingDecodingTechnique = new AES();
            this.grbData.Text = "AES Technique";
        }

        /// <summary>
        /// Handles the Click event of the BtnRC control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtnRC_Click(object sender, EventArgs e)
        {
            this.grbData.Visible = true;
            this.checkBoxAutoKey.Visible = false;
            this.encodingDecodingTechnique = new RC4();
            this.grbData.Text = "RC4 Technique";
        }

        /// <summary>
        /// Handles the Click event of the pictureBox1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void PictureBox1_Click(object sender, EventArgs e)
        {
            this.grbData.Visible = true;
            this.checkBoxAutoKey.Visible = false;
            this.encodingDecodingTechnique = new MD5();
            this.grbData.Text = "MD5 Technique";
        }

        /// <summary>
        /// Handles the Click event of the btn_extendedEc control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Btn_extendedEc_Click(object sender, EventArgs e)
        {
            try
            {
                int number = int.Parse(this.txtNumber.Text);
                int b = int.Parse(this.txtbase.Text);
                int res = HillCipher.EclidInverse(number, b);
                if (res == 0)
                {
                    this.txtMulInv.Text = "It has no mul inverse";
                    return;
                }

                while (res < 0)
                {
                    res += b;
                }

                this.txtMulInv.Text = res.ToString();
            }
            catch
            {
                MessageBox.Show("Error not in correct format");
            }
        }

        /// <summary>
        /// Handles the Click event of the pictureBox2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void PictureBox2_Click(object sender, EventArgs e)
        {
            this.grbData.Visible = true;
            this.checkBoxAutoKey.Visible = false;
            this.encodingDecodingTechnique = new RSA();
            this.grbData.Text = "RSA Technique";
        }

        /// <summary>
        /// Handles the Click event of the btDeffieHelman control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtDeffieHelman_Click(object sender, EventArgs e)
        {
            string input = this.txtInputOfDeffieHelman.Text;

            int[] temp = SecutityLib.DiffieHellman.GetKeyAtBothSides(input);

            this.txtDeffieHelmanOutput.Text = "Key at A = " + temp[0].ToString() + " Key at B = " + temp[1].ToString();
        }
    }
}
